//
//  AdvViewController.m
//  LoveDraw
//
//  Created by 梁晨 on 2017/6/13.
//  Copyright © 2017年 梁晨. All rights reserved.
//

#import "AdvViewController.h"
#import "AdvDetailViewController.h"
#import "AdvTableViewCell.h"
#import "AdvModel.h"

@interface AdvViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger page;
}
@property(nonatomic,strong)UITableView *listV;
@property(nonatomic,strong)NSMutableArray *listArr;


@end

@implementation AdvViewController

-(NSMutableArray *)listArr
{
    if (!_listArr) {
        _listArr = [NSMutableArray array];
    }
    return _listArr;
}

-(UITableView *)listV
{
    if (!_listV) {
        _listV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KscreenW, KscreenH-64) style:(UITableViewStyleGrouped)];
        _listV.delegate = self;
        _listV.dataSource = self;
        _listV.backgroundColor = [UIColor whiteColor];
        _listV.estimatedSectionHeaderHeight = 0;
        _listV.estimatedSectionFooterHeight = 0;
        [_listV registerNib:[UINib nibWithNibName:@"AdvTableViewCell" bundle:nil] forCellReuseIdentifier:@"AdvTableViewCell"];
        _listV.separatorStyle = 0;
    }
    return _listV;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.listV];
    page = 1;
    self.listV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        page++;
        [self Request];
    }];
    self.listV.mj_header = [MJRefreshNormalHeader  headerWithRefreshingBlock:^{
        [self doRefreshSenior];
    }];
    [self.listV.mj_header beginRefreshing];

}

-(void)doRefreshSenior
{
    page = 1;
    [self Request];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationItem.title = @"校园公告";
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.listArr.count == 0) {
        self.listV.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"nodata"]];
    }else
    {
        self.listV.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@""]];
    }
    return self.listArr.count;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AdvModel *model = self.listArr[indexPath.row];
    AdvTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AdvTableViewCell" forIndexPath:indexPath];
//    cell.titleL.attributedText = [AttributeString string:@"平台公告" LineSpace:1 font:18 color:UIColors(0x333333) worldSpace:1];
//    cell.DetailL.attributedText = [AttributeString string:@"关于放假的消息" LineSpace:1 font:16 color:UIColors(0x666666) worldSpace:1];
//    cell.timeL.attributedText = [AttributeString string:@"2017年5月17日" LineSpace:1 font:13 color:UIColors(0x888888) worldSpace:1];
    
    cell.titleL.text = model.title;
    cell.DetailL.text = model.Minedescription;
    cell.timeL.text = model.addtime;
    

    [cell.TitImageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KUrl,model.thumb]] placeholderImage:Kplaceholderimage];

    cell.selectionStyle = 0;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AdvModel *model = self.listArr[indexPath.row];
    AdvDetailViewController *vc = [[AdvDetailViewController alloc]init];
    vc.aid = model.aid;
    [self.navigationController pushViewController:vc animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return FLT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return FLT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

-(void)Request
{
    [RequestManager RequstWithUrl:@"/index.php?g=App&m=Index&a=noticeList" parmDic:@{@"page":[NSString stringWithFormat:@"%ld",page]} requestType:RequestGET Success:^(id data)
    {
        NSArray *Arr = [AdvModel modelConfigure:data];
        if (page == 1) {
            [self.listArr removeAllObjects];
        }
        if (Arr.count==0) {
            page--;
        }
        [self.listArr addObjectsFromArray:Arr];
        [self.listV reloadData];
        [self.listV.mj_footer endRefreshing];
        [self.listV.mj_header endRefreshing];
        if (Arr.count == 0||Arr.count<10)
        {
            self.listV.mj_footer.state = MJRefreshStateNoMoreData;
        }

    } error:^(NSError *error)
    {

    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
